package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.PageParam;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    //注入service
    @Autowired
    private EmployeeService es;

    /**
     * 更新员工
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R update(@RequestBody Employee employee,HttpSession session){
        Employee employee1 = (Employee) session.getAttribute("employee");
        int row = es.update(employee, employee1.getId());
        if (row>0){
            return R.success("更新状态成功");
        }else {
            return R.error("更新状态失败");
        }
    }

    /**
     * 分页查询
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R<IPage<Employee>> findPage(PageParam pageParam){
        try {
            IPage<Employee> page = es.findPage(pageParam);
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("分页查询失败");
        }

    }


    /**
     * 添加员工
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R add(@RequestBody Employee employee,HttpSession session){
        Employee employee1 = (Employee) session.getAttribute("employee");
        int row = es.add(employee, employee1.getId());
        if (row>0){
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }


    }

}
